package com.zhouyibike.uu.ui.activity.yihoudong;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.GetYiTransferResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.listener.MyListener;
import com.zhouyibike.uu.ui.adapter.YihuodongAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.ui.view.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zy on 2017/10/11.
 */
public class YihuodonglistFragment extends Fragment implements View.OnClickListener {
    private TextView jiaoyi_zhuanrangbt, jiaoyi_rengoubt;
    private LinearLayout ll_nothing;
    private PullToRefreshLayout pullToRefreshLayout;
    private ListView lv_allmsg;
    private int refreshtype = 0;
    private int page = 1;//页码
    private boolean ismore = true;//判断是否还有更多数据
    private List<GetYiTransferResult.DataBean> alllist = new ArrayList<>();
    private YihuodongAdapter mydetailAdapter;
    private String type = "1";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle b = getArguments();
        View view = View.inflate(getActivity(), R.layout.fragment_myyiquanlist, null);
        //  type=getIntent().getStringExtra("type");
        initView(view);
        initlistener();
        dataf();
        return view;
    }

    private void initlistener() {
        jiaoyi_rengoubt.setOnClickListener(this);
        jiaoyi_zhuanrangbt.setOnClickListener(this);
    }

    private void initView(View view) {
        jiaoyi_rengoubt = view.findViewById(R.id.jiaoyi_rengoubt);
        jiaoyi_zhuanrangbt = view.findViewById(R.id.jiaoyi_zhuanrangbt);
        pullToRefreshLayout = ((PullToRefreshLayout) view.findViewById(R.id.refresh_view));
        pullToRefreshLayout.setOnRefreshListener(new MyListener2());
        ll_nothing = (LinearLayout) view.findViewById(R.id.ll_nothing_show);
        lv_allmsg = (ListView) view.findViewById(R.id.content_view);
    }

    private void getMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", ((BaseActivity) getActivity()).preferences.getString("token", ""));
        params.put("pageIndex", page + "");
        ((BaseActivity) getActivity()).addSubscription(((BaseActivity) getActivity()).apiStores.GetYiTransfer(params),
                new ApiCallback<GetYiTransferResult>() {

                    @Override
                    public void onSuccess(GetYiTransferResult model) {
//                        String string=new Gson().toJson(model);
//                        Log.e("onSuccess: ",string );
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            if (refreshtype == 1) {
                                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                                alllist.clear();
                            } else if (refreshtype == 2) {
                                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                            }
                            if (model.getData().size() == 0) {
                                ismore = false;
                            }
//                            for (int i = 0; i <10 ; i++) {
//                                alllist.addAll(model.getData());
//                            }
                            alllist.addAll(model.getData());

                            if (alllist.size() == 0) {
//                                rl.setVisibility(View.GONE);
                                pullToRefreshLayout.setVisibility(View.INVISIBLE);
                                ll_nothing.setVisibility(View.VISIBLE);
                            } else {
//                                rl.setVisibility(View.VISIBLE);
                                pullToRefreshLayout.setVisibility(View.VISIBLE);
                                ll_nothing.setVisibility(View.GONE);
                            }
                            initData();

                        } else {
                            ((BaseActivity) getActivity()).toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
//                        toastShow(msg);
//                        rl.setVisibility(View.GONE);
//                        pullToRefreshLayout.setVisibility(View.INVISIBLE);

                    }

                    @Override
                    public void onFinish() {
//                        loading.setVisibility(View.GONE);
//                        noyiquantv.setVisibility(View.VISIBLE);
//                        noyiquaniv.setVisibility(View.VISIBLE);
                        // ll_nothing.setVisibility(View.GONE);
                        //  dismissProgressDialog();
                    }
                });
    }

    private void initData() {
        if (page == 1) {
            mydetailAdapter = new YihuodongAdapter(getActivity(), alllist);
            lv_allmsg.setAdapter(mydetailAdapter);
        } else {
            mydetailAdapter.setList(alllist);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.bar_btn_back:
//                finish();
//                break;
            case R.id.jiaoyi_rengoubt:
                Intent intent = new Intent(getActivity(), YijuangounaiActivity.class);//跳转购买100
                startActivityForResult(intent, 188);
                break;
            case R.id.jiaoyi_zhuanrangbt:
                Intent intent2 = new Intent(getActivity(), YihuodongzhuanrangActivity.class);//跳转转让200
                if (((YihuodongActivity3) getActivity()).f1 != null) {
                    intent2.putExtra("cansellnum", ((YihuodongActivity3) getActivity()).f1.cansellyiquan);
                } else {
                    intent2.putExtra("cansellnum", 0);
                }
                startActivityForResult(intent2, 188);
                break;
            default:
                break;
        }
    }

    class MyListener2 extends MyListener {
        @Override
        public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
            super.onRefresh(pullToRefreshLayout);
            refreshtype = 1;
            page = 1;
            ismore = true;
            getMsg();
        }

        @Override
        public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            super.onLoadMore(pullToRefreshLayout);
            refreshtype = 2;
            if (ismore) {
                page++;
                getMsg();
            } else {
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }
    }

    public void dataf() {
        refreshtype = 1;
        page = 1;
        ismore = true;
        getMsg();
    }
}
